using System;
using System.Collections.Generic;
using BCWAccountVip;
using I2.Loc;
using UnityEngine;

public class PanelResultRewardUI : MonoBehaviour
{
	private GameObject panelAccountVipBuy;

	[SerializeField]
	private GameObject panelRewardExample;

	[SerializeField]
	private UIGrid grid;

	private Vector3 _startPosition;

	private int _bonusFactorAward;

	private bool _isActiveVip;

	private List<InfoMatchReward> _rewards = new List<InfoMatchReward>();

	private List<InfoMatchReward> _rewardsVip = new List<InfoMatchReward>();

	private List<InfoMatchReward> _rewardsNoVip = new List<InfoMatchReward>();

	private GameObject _noVipReward;

	private GameObject _vipReward;

	private bool _blockGiveAward;

	private bool _isCalculate;

	public Vector3 StartPosition
	{
		set
		{
			_startPosition = value;
		}
	}

	public int BonusFactorAward
	{
		set
		{
			_bonusFactorAward = value;
		}
	}

	public bool IsActiveVip
	{
		set
		{
			_isActiveVip = value;
		}
	}

	public List<InfoMatchReward> Rewards
	{
		get
		{
			return _rewards;
		}
	}

	public static event Action giveReward;

	private void Awake()
	{
		panelAccountVipBuy = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("Prefabs/AccountVipWindow"));
		panelAccountVipBuy.transform.SetParent(base.transform.parent);
		panelAccountVipBuy.transform.localScale = Vector3.one;
		panelAccountVipBuy.transform.localPosition = Vector3.zero;
		panelAccountVipBuy.gameObject.SetActive(false);
	}

	private void OnEnable()
	{
		InAppShop.eventBuyAccount += BuyAccountVipSuccsesful;
	}

	private void OnDisable()
	{
		InAppShop.eventBuyAccount -= BuyAccountVipSuccsesful;
		Clear();
	}

	public void BuyAccountVipSuccsesful()
	{
		_isActiveVip = true;
		PanelResultReward component = _noVipReward.GetComponent<PanelResultReward>();
		component.ActiveFrame.SetActive(false);
		PanelResultReward component2 = _vipReward.GetComponent<PanelResultReward>();
		component2.ActiveFrame.SetActive(true);
		component2.ButtonBuyVip.SetActive(false);
		component2.LabelBuyVip.SetActive(true);
	}

	public void Show()
	{
		CalculateReward();
		_noVipReward = UnityEngine.Object.Instantiate(panelRewardExample);
		_noVipReward.transform.SetParent(grid.transform);
		_noVipReward.transform.localScale = Vector3.one;
		_noVipReward.SetActive(true);
		PanelResultReward component = _noVipReward.GetComponent<PanelResultReward>();
		component.NoVipBG.SetActive(true);
		component.ActiveFrame.SetActive(!_isActiveVip);
		component.BonusPanel.SetActive(_bonusFactorAward > 1);
		component.SetTitle(ScriptLocalization.Get("Game_Common/vip_acc_withoutVipAcc"));
		component.ShowRewards(_rewards);
		_vipReward = UnityEngine.Object.Instantiate(panelRewardExample);
		_vipReward.transform.SetParent(grid.transform);
		_vipReward.transform.localScale = Vector3.one;
		_vipReward.SetActive(true);
		PanelResultReward component2 = _vipReward.GetComponent<PanelResultReward>();
		component2.VipBG.SetActive(true);
		component2.BonusPanel.SetActive(_bonusFactorAward > 1);
		component2.SetTitle(ScriptLocalization.Get("Game_Common/vip_acc_withVipAcc"));
		component2.ShowRewards(_rewardsVip);
		component2.ActiveFrame.SetActive(_isActiveVip);
		component2.LabelBuyVip.SetActive(_isActiveVip);
		component2.ButtonBuyVip.SetActive(!_isActiveVip);
		grid.repositionNow = true;
	}

	public void GiveReward()
	{
		if (_blockGiveAward)
		{
			return;
		}
		_blockGiveAward = true;
		List<InfoMatchReward> list = ((!_isActiveVip) ? _rewards : _rewardsVip);
		foreach (InfoMatchReward item in list)
		{
			switch (item.id)
			{
			case "cash":
				settings.ShowSpecialLabelWithAnimation(item.count, _startPosition, false);
				break;
			case "gold":
				settings.ShowLabelWithAnimation(item.count, _startPosition, false);
				break;
			case "star":
				CupsManager.Cups += item.count;
				LeaderBoardServer.instance.SendStars(item.count, GameController.instance.curTypeGame);
				break;
			default:
				MarketController.Instance.BuyProduct(MarketController.Instance.GetProduct(item.id), true, item.count);
				break;
			}
		}
		if (_bonusFactorAward > 1)
		{
			BonusAwardManager.instance.SendBonusAwarded(GameController.instance.curTypeGame);
		}
		if (PanelResultRewardUI.giveReward != null)
		{
			PanelResultRewardUI.giveReward();
		}
		Clear();
	}

	private void CalculateReward()
	{
		if (_isCalculate)
		{
			return;
		}
		for (int i = 0; i < _rewards.Count; i++)
		{
			InfoMatchReward value = _rewards[i];
			if (value.id != "star")
			{
				value.count = Mathf.Max(value.count, value.count * _bonusFactorAward);
			}
			_rewards[i] = value;
		}
		foreach (InfoMatchReward reward in _rewards)
		{
			_rewardsVip.Add(AccountVipBonusChanger.GetInstance().ModifyReward(reward));
		}
		_isCalculate = true;
	}

	public void BuyAccountVip()
	{
		panelAccountVipBuy.SetActive(true);
	}

	private void Clear()
	{
		if (_noVipReward != null)
		{
			UnityEngine.Object.Destroy(_noVipReward);
		}
		if (_noVipReward != null)
		{
			UnityEngine.Object.Destroy(_vipReward);
		}
		_rewards.Clear();
		_rewardsVip.Clear();
		_isCalculate = false;
		_blockGiveAward = false;
	}
}
